#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#define ARR_SIZE    (11) 

static int32_t sum_recursive(uint32_t *data, uint32_t size)
{
    uint32_t stride = 0;
    uint32_t i = 0;

    if(1 >= size)
    {
        return data[0];
    }

    stride = size / 2;

    for(i = 0; i < stride; i++)
    {
        data[i] += data[i + stride];
    }

    //如果元素个数为奇数，那么就将最后一个元素和第一个元素相加
    if(0 != (size & 0x01))
    {
        data[0] += data[size - 1];
    }

    return sum_recursive(data, stride);
}


int main(int argc, char *argv[])
{
    uint32_t a[ARR_SIZE] = {0};
    uint32_t i = 0;
    uint32_t sum = 0;

    for(i = 0; i < ARR_SIZE; i++)
    {
        a[i] = i;
    }

    sum = sum_recursive(a, ARR_SIZE);
    printf("sum = %u\n", sum);

    return 0;
}
